package cn.edu.ctbu.scoremg.api;


import cn.edu.ctbu.scoremg.constant.REnum;
import cn.edu.ctbu.scoremg.entity.Student;
import cn.edu.ctbu.scoremg.exception.RException;
import cn.edu.ctbu.scoremg.service.StudentService;
import cn.edu.ctbu.scoremg.util.RUtil;
import cn.edu.ctbu.scoremg.vo.QueryObj;
import cn.edu.ctbu.scoremg.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
public class StudentApiController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public R<List<Student>> findAll() {
        List<Student> students = studentService.getAll();
        return RUtil.success(students);
    }
    @GetMapping("/list0")
    public R<List<Student>> findAll0() {
        List<Student> students = studentService.getAll();
        return RUtil.success(students);
        //此时是当学生登录时
    }

    @GetMapping("/{id}")
    public R<Student> findById(@PathVariable int id) {
        Student student = studentService.findById(id);
        return RUtil.success(student);
    }

    @GetMapping("/findByName")
    public R<List<Student>> findByName(@RequestParam String name) {
        return RUtil.success(studentService.findByName(name));
    }

    @PostMapping("/add")
    public R<Student> add(@RequestBody Student student) {
        return RUtil.success(studentService.add(student));
    }

    @PutMapping("/update")
    public R<Student> update(@RequestBody Student student) {
        return RUtil.success(studentService.update(student));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        studentService.delete(id);
        return RUtil.success();
    }

    @GetMapping("/validateUser")
    public R validateNoAndPassword(String no, String password) throws Exception {
        return RUtil.success(studentService.validateUsernameAndPassword(no, password));
    }
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        try {
            studentService.deleteBatch(ids);
            return RUtil.success();
        } catch (Exception e) {
            return RUtil.error(REnum.DELETE_ERR.getCode(), "批量删除失败");
        }
    }

    @PostMapping("/getbypage")
    public R<Page<Student>> getByPage(@RequestBody QueryObj<Student> qObj) {
        System.out.println("接收到的请求数据: " + qObj); // 调试输出请求数据

        // 按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        Integer pageIndex = 0;
        Integer pageSize = 10; // 一页10条数据

        if (qObj == null) {
            // student事例为空就直接调用分页
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            Page<Student> students = studentService.getAll(pageable);
            return RUtil.success(students.getContent(), students.getTotalElements());
        } else {
            pageIndex = qObj.getPage() - 1; // 至少是1页
            pageSize = qObj.getLimit();

            if (qObj.getData() instanceof Student) { // 示例查询
                Student student = (Student) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

                ExampleMatcher matcher = ExampleMatcher.matching() // 生成示例匹配对象
                        // 匹配条件
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        // 忽略空值
                        .withIgnoreNullValues();

                Example<Student> example = Example.of(student, matcher);
                Page<Student> studentPage = studentService.getAll(example, pageable);

                return RUtil.success(studentPage.getContent(), studentPage.getTotalElements());
            } else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}